package com.qk.management.service.impl;

import com.qk.common.PageResult;


import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }
    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteById(id);
    }



    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }


    @Override
    public Role getRoleById(Integer id) {
        return roleMapper.selectById(id);
    }
    @Override
    public void addRole(Role role) {

        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);

    }


    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pageSize) {

            Integer total = roleMapper.count(name, label);
            Integer offset = (page - 1) * pageSize;
            List<Role> rows = roleMapper.selectByPage(name, label, offset, pageSize);
            return PageResult.<Role>builder().total(total).rows(rows).build();
        }
    }
